package client.service;


import javafx.scene.control.Alert;
import client.main.Client_main;
import model.Message;
import model.MsgType;
import model.UserReg;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.regex.Pattern;
import com.google.gson.Gson;

//对用户输入的登录信息进行校验，符合格式后发送到服务器，并接收校验结果将其返回
public class RegUser {
    private Socket client;
    private DataOutputStream output;
    private DataInputStream input;

    public RegUser() {
        
    }

    //检测用户输入信息是否合理,合理则将注册信息封存到对象中返回,不合理则显示提示信息
    private UserReg checkLoginInfo(String uname, String pwd) {
        UserReg u = null;
        if (!uname.equals("") && !pwd.equals("")) {
            String pattern = "[\\u4E00-\\u9FA5a-zA-Z0-9]{1,10}";
            String patterns = "[a-zA-Z0-9]{1,10}";
            if (Pattern.matches(patterns, pwd)) {
                if (Pattern.matches(pattern, uname)) {
                    u = new UserReg(uname, pwd);
                } else {//账号不合法
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("用户名为1到10位中文、字母或数字，请重新输入！");
                    alert.show();
                }
            } else {//密码不合法
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("密码为1到10位英文字母或数字，请重新输入！");
                alert.show();
            }

        } else if (uname.equals("")) {//用户名为空
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setContentText("请输入用户名！");
            alert.show();
        } else if (pwd.equals("")) {//密码为空
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setContentText("请输入密码！");
            alert.show();
        }
        return u;
    }


    //将通过校验的登录信息发送到服务器并将得到的消息包返回(包含当前用户的所有好友)
    public Message sendRegInfoToServer(String uname, String pwd) {
    	
    	try {
            client = new Socket(Client_main.ip, 12345);
            output = new DataOutputStream(client.getOutputStream());
            input = new DataInputStream(client.getInputStream());
            System.out.println("初始化LoginUserReg");
        } catch (IOException e) {
            System.out.println("连接服务器失败！");
            e.printStackTrace();
        }
    	   	
        UserReg u = checkLoginInfo(uname, pwd);
        Message mes = new Message();
        if(u != null) {
        	try {
        		 Gson gson = new Gson();
        		 mes.setType(MsgType.REGISTER);
        		 mes.setTime(u.getuname());   //复用time属性来传输用户昵称
        		 mes.setText(u.getPwd());     //复用text属性来传输用户密码
        		 
        		 String json = gson.toJson(mes);
        		 output.writeUTF(json);
        		 
        		 json = input.readUTF();
        		 input.close();
        		 mes = gson.fromJson(json, Message.class);
        		 if(mes.getType() == MsgType.REG_SUCCEED) {
        			 return mes;
        		 }else if(mes.getType() == MsgType.REG_FAILED) {
        			 Alert alert = new Alert(Alert.AlertType.WARNING);
                     alert.setContentText("注册失败，请重试！");
                     alert.show();
        		 }

        	}catch(IOException e) {
        		e.printStackTrace();
        	}
        	
        }
               
        return null;
    }
}